import { Link, useHistory } from "react-router-dom"
import useList from "./useList"
import { Card, Breadcrumb, Form, Button, Radio, DatePicker, Select, Table, Tag, Space } from "antd"
import { EditOutlined, DeleteOutlined } from "@ant-design/icons"
import img404 from "@/assets/images/login.png"
const { Option } = Select
const { RangePicker } = DatePicker
const Article = () => {
  const history = useHistory()
  const columns = [
    {
      title: "封面",
      dataIndex: "cover",
      render: (cover) => {
        return <img src={cover || img404} width={200} height={150} alt="" />
      }
    },
    {
      title: "标题",
      dataIndex: "title",
      width: 220
    },
    {
      title: "状态",
      dataIndex: "status",
      render: (data) => <Tag color={articleStatus[data].color}>{articleStatus[data].text}</Tag>
    },
    {
      title: "发布时间",
      dataIndex: "pubdate"
    },
    {
      title: "阅读数",
      dataIndex: "read_count"
    },
    {
      title: "评论数",
      dataIndex: "comment_count"
    },
    {
      title: "点赞数",
      dataIndex: "like_count"
    },
    {
      title: "操作",
      render: (data) => {
        return (
          <Space size="middle">
            <Button type="primary" shape="circle" icon={<EditOutlined />} onClick={() => history.push(`/home/publish/${data.id}`)} />
            <Button type="primary" danger shape="circle" icon={<DeleteOutlined />} onClick={() => delArticle(data.id)} />
          </Space>
        )
      }
    }
  ]

  const { channels, page, pageSize, count, list, articleStatus, onSearch, changePage, delArticle } = useList()
  return (
    <div>
      <Card
        title={
          <Breadcrumb separator=">">
            <Breadcrumb.Item>
              <Link to="/home">首页</Link>
            </Breadcrumb.Item>
            <Breadcrumb.Item>内容管理</Breadcrumb.Item>
          </Breadcrumb>
        }
        style={{ marginBottom: 20 }}>
        <Form onFinish={onSearch} initialValues={{ status: -1 }}>
          <Form.Item label="状态" name="status">
            <Radio.Group>
              <Radio value={-1}>全部</Radio>
              <Radio value={0}>草稿</Radio>
              <Radio value={1}>待审核</Radio>
              <Radio value={2}>审核通过</Radio>
              <Radio value={3}>审核失败</Radio>
            </Radio.Group>
          </Form.Item>

          <Form.Item label="频道" name="channel_id">
            <Select placeholder="请选择文章频道" style={{ width: 120 }}>
              {channels.map((item) => (
                <Option key={item.id} value={item.id}>
                  {item.name}
                </Option>
              ))}
            </Select>
          </Form.Item>

          <Form.Item label="日期" name="date">
            <RangePicker></RangePicker>
          </Form.Item>

          <Form.Item>
            <Button type="primary" htmlType="submit">
              筛选
            </Button>
          </Form.Item>
        </Form>
      </Card>
      <Card title={`根据筛选条件共查询到 ${count} 条结果：`}>
        <Table
          columns={columns}
          dataSource={list}
          rowKey="id"
          pagination={{
            current: page,
            pageSize,
            total: count,
            showSizeChanger: true,
            position: ["bottomCenter"],
            onChange: changePage
          }}
        />
      </Card>
    </div>
  )
}

export default Article
